package com.ruge._06_springboot_swagger.controller;

import com.ruge._06_springboot_swagger.bean.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.*;


@RestController
/**
 * @Api()
 * 用于类；表示标识这个类是swagger的资源
 * tags–表示说明
 * value–也是说明，可以使用tags替代
 */
@Api(value = "用户controller", tags = {"用户操作接口"})
public class UserController {


    /**
     * @ApiOperation() 用于方法；表示一个http请求的操作
     * value用于方法描述
     * notes用于提示内容
     * tags可以重新分组（视情况而用）
     */
    /**
     * @ApiParam() 用于方法，参数，字段说明；表示对参数的添加元数据（说明或是否必填等）
     * name–参数名
     * value–参数说明
     * required–是否必填
     */
    /**
     * 使用@RequestParam时，URL是这样的：http://host:port/path?参数名=参数值
     * <p>
     * 使用@PathVariable时，URL是这样的：http://host:port/path/参数值
     */
    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @RequestMapping(value = "user/{id}", method = RequestMethod.GET)
    public User getUserById(@ApiParam(name = "id", value = "用户id", required = true) @PathVariable int id) {
        User user = new User();
        user.setId(id);
        return user;
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @RequestMapping(value = "users", method = RequestMethod.GET)
    public List<User> getUserList() {
        return Arrays.asList(
                new User(1, "张1", new Date(System.currentTimeMillis())),
                new User(2, "张2", new Date(System.currentTimeMillis())),
                new User(3, "张3", new Date(System.currentTimeMillis()))
        );
    }

    /**
     * paramType：表示参数放在哪个地方
     * header-->请求参数的获取：@RequestHeader(代码中接收注解)
     * query-->请求参数的获取：@RequestParam(代码中接收注解)
     * path（用于restful接口）-->请求参数的获取：@PathVariable(代码中接收注解)
     * body-->请求参数的获取：@RequestBody(代码中接收注解)
     */

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "User's name", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "birthday", value = "User's birthday", required = false, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "User ID", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "user", method = RequestMethod.POST)
    public User add(User user) {
        return user;
    }

}